package com.exm.sql.util;

import com.alibaba.fastjson2.JSON;
import com.exm.sql.config.RedisKeyDic;
import com.exm.sql.config.TokenContext;
import com.exm.sql.exception.ToeknExpirationException;
import com.exm.sql.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserUtil {

    @Autowired
    RedisUtils redisUtils;

    public String getUserAccountInfoByToken(){
        String token = TokenContext.getToken();
        if (token==null) throw new ToeknExpirationException();
        User user = JSON.parseObject(redisUtils.getString(RedisKeyDic.USER_TOKEN.getFormattedKey(token)),User.class);
        if(user==null) throw new ToeknExpirationException();
        return user.getAccount();
    }
}
